package main;

import po.entity.Indicator;
import po.entity.Param;
import po.entity.Rule;
import po.enums.Operator;
import po.enums.ParamValueType;
import po.tree.Tree;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by guowei on 2019/6/9.
 */
public class Main {
    public static void main(String[] args) {
        Param[] params = prepareParam();
        Indicator[] indicators = prepareIndicators();
        Rule rule1 = new Rule("rule1", "rule1");
        Rule rule2 = new Rule("rule2", "rule2");
        Rule rule3 = new Rule("rule3", "rule3");
        Rule rule4 = new Rule("rule4", "rule4");

        // rule4
        rule4.addSubRule(indicators[4]);
        rule4.addParam(params[4]);

        // rule2
        rule2.addSubRule(indicators[3]);
        rule2.addParam(params[0]);
        rule2.addParam(params[1]);
        rule2.addParam(params[3]);

        // rule3
        rule3.addSubRule(rule4);
        rule3.addOperator(Operator.AND);
        rule3.addSubRule(indicators[1]);
        rule3.addOperator(Operator.OR);
        rule3.addSubRule(indicators[2]);
        rule3.addParam(params[4]);

        rule1.addSubRule(rule2);
        rule1.addOperator(Operator.AND);
        rule1.addSubRule(rule3);
        rule1.addOperator(Operator.AND);
        rule1.addSubRule(indicators[0]);
        rule1.addOperator(Operator.OR);
        rule1.addSubRule(indicators[1]);
        rule1.addParam(params[2]);

        Tree tree = new Tree(rule1);
        try {
            boolean hasConflicts = tree.checkConflicts();
            System.out.println("============= Result: " + (hasConflicts?"CONFLICTS":"COMPATIBLE") + " =============");
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

    private static Param[] prepareParam() {
        String paramValue1 = "paramValue1";
        String paramValue2 = "paramValue2";
        String paramValue3 = "paramValue3";
        String paramValue4 = "paramValue4";
        String paramValue5 = "paramValue5";
        Param p1 = new Param("param1", "param1", ParamValueType.SCOPE, new ArrayList<>(), 100.0, 10.0);
        List<String> param2Value = new ArrayList<>();
        param2Value.add(paramValue1);
        param2Value.add(paramValue2);
        param2Value.add(paramValue3);
        Param p2 = new Param("param2", "param2", ParamValueType.ENUM, param2Value, 0, 0);
        List<String> param3Value = new ArrayList<>();
        param3Value.add(paramValue1);
        param3Value.add(paramValue2);
        param3Value.add(paramValue4);
        param3Value.add(paramValue5);
        Param p3 = new Param("param2", "param2", ParamValueType.ENUM, param3Value, 0, 0);
        Param p4 = new Param("param1", "param1", ParamValueType.SCOPE, new ArrayList<>(), 200.0, 50.0);
        Param p5 = new Param("param1", "param1", ParamValueType.SCOPE, new ArrayList<>(), 90.0, 70.0);
        Param[] params = {p1, p2, p3, p4, p5};
        return params;
    }

    private static Indicator[] prepareIndicators() {
        String indicatorValue1 = "indicatorValue1";
        String indicatorValue2 = "indicatorValue2";
        String indicatorValue3 = "indicatorValue3";
        String indicatorValue4 = "indicatorValue4";
        String indicatorValue5 = "indicatorValue5";
        Indicator i1 = new Indicator("indicator1", "indicator1", ParamValueType.SCOPE, new ArrayList<>(), 100.0, 10.0);
        List<String> indicator2Value = new ArrayList<>();
        indicator2Value.add(indicatorValue1);
        indicator2Value.add(indicatorValue3);
        indicator2Value.add(indicatorValue5);
        Indicator i2 = new Indicator("indicator2", "indicator2", ParamValueType.ENUM, indicator2Value, 0, 0);
        List<String> indicator3Value = new ArrayList<>();
        indicator3Value.add(indicatorValue1);
        indicator3Value.add(indicatorValue2);
        indicator3Value.add(indicatorValue4);
        Indicator i3 = new Indicator("indicator2", "indicator2", ParamValueType.ENUM, indicator3Value, 0, 0);
        Indicator i4 = new Indicator("indicator1", "indicator1", ParamValueType.SCOPE, new ArrayList<>(), 200.0, 80.0);
        Indicator i5 = new Indicator("indicator1", "indicator1", ParamValueType.SCOPE, new ArrayList<>(), 90.0, 7.0);
        Indicator[] indicators = {i1, i2, i3, i4, i5};
        return indicators;
    }
}
